from rest_framework import mixins
from rest_framework import viewsets
from rest_framework import status
from rest_framework.response import Response
from .serializers import ApplyListSerializers, VisitApplicantSerializers
from apps.product.models import Product
from apps.sales_man.views import ApplicantList
from apps.utils.views import YouCardApi
from apps.sales_man.models import Single
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import ensure_csrf_cookie


# class VisitProcessViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
#     """
#     查看进度
#     """
#


# class ApplyViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
#     """
#     操作申请人接口
#     create: 添加申请人列表
#     """
#     serializer_class = ApplyListSerializers
#
#     def perform_create(self, serializer):
#         return serializer.save()
#
#     def create(self, request, *args, **kwargs):
#         serializer = self.get_serializer(data=request.data)
#         serializer.is_valid(raise_exception=True)
#         obj = self.perform_create(serializer)
#         apply_id = obj.id
#         headers = self.get_success_headers(serializer.data)
#         re_dict = {"apply_id": apply_id}
#         return Response(re_dict, status=status.HTTP_201_CREATED, headers=headers)


class VisitApplicantViewSet(mixins.CreateModelMixin, viewsets.GenericViewSet):
    """
    测试
        create: 必须是思兮接口才有(有productAPI)
    """
    serializer_class = VisitApplicantSerializers

    def perform_create(self, serializer, visit_id=None):
        instance = serializer.save()
        instance.visit_id = visit_id
        instance.save()

    def create(self, request, *args, **kwargs):
        data = request.data
        applicant = data.get("applicant")
        product = data.get("product")
        product_obj = Product.objects.get(id=product)
        apply_obj = ApplicantList.objects.get(id=applicant)
        try:
            t_user = apply_obj.salesman.leader_id
        except Exception as e:
            print(e)
            t_user = None
        Single.objects.create(
            product_id=product,
            applicant_id=applicant,
            b_user=apply_obj.salesman,
            t_user_id=t_user
        )
        product_id = product_obj.productapiproduct.api_product  # 思兮 api id
        username = apply_obj.name
        phone = apply_obj.tel
        id_card = apply_obj.card
        card_api = YouCardApi()
        content = card_api.apply_card(product_id, username, phone, id_card)
        if content.get("code") == 0:
            request.POST._mutable = True
            data["visit_id"] = content.get("visit_id")
        serializer = self.get_serializer(data=data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer, visit_id=data["visit_id"])
        headers = self.get_success_headers(serializer.data)
        url = content.get("bank_url")
        re_dict = {"url": url}
        return Response(re_dict, status=status.HTTP_201_CREATED, headers=headers)
